package vip.xiaonuo.biz.modular.Englishword;

import cn.hutool.core.util.ObjectUtil;
import edu.stanford.nlp.pipeline.*;
import org.apache.commons.lang3.StringUtils;

import java.util.Properties;
import java.util.ArrayList;
import java.util.List;

public class NLPUtils{
    public static String splitIntoParagraphs(String text,StanfordCoreNLP stanfordCoreNLP){
        List<String> list = splitIntoParagraph(text,stanfordCoreNLP);
        return "      "+StringUtils.join(list, "      \n      ");
    }

    private static List<String> splitIntoParagraph(String text,StanfordCoreNLP stanfordCoreNLP) {
        // 设置环境
        Properties props = new Properties();
        // 启用分词器和句子分割器
        props.setProperty("annotators", "tokenize,ssplit");
        if (ObjectUtil.isEmpty(stanfordCoreNLP)){
            stanfordCoreNLP = new StanfordCoreNLP(props);
        }

        // 创建一个空的注解对象，包含输入文本
        CoreDocument document = new CoreDocument(text);
        stanfordCoreNLP.annotate(document);

        List<String> paragraphs = new ArrayList<>();
        StringBuilder paragraph = new StringBuilder();

        for (CoreSentence sentence : document.sentences()) {
            String sentenceText = sentence.toString();

            // 如果当前段落长度加上新句子长度超过某个阈值，则开始新的段落
            if ((paragraph.length() + sentenceText.length()) > 300) {
                paragraphs.add(paragraph.toString().trim());
                paragraph = new StringBuilder();
            }

            paragraph.append(sentenceText).append(" ");
        }

        // 添加最后一个段落
        if (!paragraph.isEmpty()) {
            paragraphs.add(paragraph.toString().trim());
        }

        return paragraphs;
    }

    public static void main(String[] args) {
        String text = "In the world of scientific exploration, the telescope has played an indispensable role in expanding our understanding of the universe. Recently, astronomers have been using advanced telescopes to observe distant galaxies and celestial bodies, gaining unique insights into the mysteries of space. The development of telescopes has not only provided scientists with the ability to see beyond what is visible to the naked eye but also equipped them with tools to calculate distances and sizes of celestial objects accurately. Telescopes have helped researchers detect X-rays and other forms of radiation from stars and black holes, which are often hidden by cosmic dust and gas. This has led to unexpected discoveries that challenge conventional theories about the universe. In tropical regions, where atmospheric conditions can be challenging for ground-based telescopes, observatories are strategically placed at high altitudes to minimize interference. These advancements in technology have allowed us to conquer some of the limitations imposed by Earth's atmosphere, opening up new frontiers in astronomy. However, the use of telescopes also raises concerns about the potential impact on local ecosystems and preventive measures must be taken to ensure that these instruments do not disrupt natural habitats or provoke environmental chaos."; // 替换为你的实际文本
        String paragraphs = splitIntoParagraphs(text,null);

        System.out.println(paragraphs);
    }
}